//
//  EpointSingleChooseAlertView.m
//  KeyBoardControlDemo
//
//  Created by 李亚春 on 13-6-18.
//  Copyright (c) 2013年 Aladdin. All rights reserved.
//

#import "EpointSingleChooseAlertView.h"

@interface EpointSingleChooseAlertView ()
{
    UITableView *tv;
    NSMutableArray *dataArr;
}
@end

@implementation EpointSingleChooseAlertView

- (id)initWithArray:(NSMutableArray *)arr title:(NSString *)_title;
{
    
    CGFloat frame_height = [[UIScreen mainScreen]bounds].size.height-64;
    CGFloat midy = frame_height/2-380/2;
    self = [super initWithFrame:CGRectMake(0, 0, 320, frame_height)];
    if (self) {
        [self setBackgroundColor:[UIColor colorWithRed:191/255.0f green:191/255.0f blue:191/255.0f alpha:0.8f]];
        dataArr = [arr retain];
        
        
        UIView *alertFrame = [[UIView alloc]initWithFrame:CGRectMake(20, midy, 280, 380)];
        [alertFrame setBackgroundColor:[UIColor grayColor]];
        
        tv = [[UITableView alloc]initWithFrame:CGRectMake(0, 44, 280, 336)];
        [tv setDataSource:self];
        [tv setDelegate:self];
        [alertFrame addSubview:tv];
        
        UILabel *lbTitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 280, 44)];
        [lbTitle setTextColor:[UIColor whiteColor]];
        [lbTitle setBackgroundColor:[UIColor clearColor]];
        [lbTitle setText:_title];
        [alertFrame addSubview:lbTitle];
        [lbTitle release];
        
        [self addSubview:alertFrame];
        [alertFrame release];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"simplecell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]init];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%@",[dataArr objectAtIndex:indexPath.row]];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArr.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self removeFromSuperview];
    self.selectedItem = [NSString stringWithFormat:@"%@",[dataArr objectAtIndex:indexPath.row]];
    self.selectedIndex = indexPath.row;
    [self.delegate epointSingleChooseViewEndSelected:self];
}

@end
